﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace PhotoDownloader
{
    public partial class Form1 : Form
    {
        private string readMoreLink = "<a class=\"readmore-link\" href=\"";
        private string address = "www.ultras-tifo.net";

        private SitesMerger siteMerger;

        public Form1()
        {
            InitializeComponent();

        }

        private void buttonCheckNews_Click(object sender, EventArgs e)
        {
            labelState.Text = "Pobieranie";

            string htmlCode = CodeAnalyzer.DownloadCode(txtSiteAddress.Text);

            List<string> list = CodeAnalyzer.FindPhrases(htmlCode, readMoreLink, "\"");
            foreach (string el in list)
            {
                linkBindingSource.Add(new Link(el));
            }

            labelState.Text = "Pobrano";

        }

        private void buttonImagesSearching_Click(object sender, EventArgs e)
        {
            List<string> links = new List<string>();

            foreach (DataGridViewRow link in dataGridView1.Rows)
            {
                try
                {
                    // 0 - name
                    // 1 - isChecked
                    if (link.Cells[1].Value.Equals(true))
                    {
                        links.Add(txtSiteAddress.Text + link.Cells[0].Value.ToString());
                    }
                }
                catch (NullReferenceException) { }
            }

            siteMerger = new SitesMerger(links);

            List<string> images = siteMerger.FindImages();

            foreach (string img in images)
            {
                Console.WriteLine(img);
            }

            Console.WriteLine("Searching FINISH");

            siteMerger.DownloadFiles(images, address);
        }

    }
}
